const Base = require('./base.js');

module.exports = class extends Base {
  /**
  * index action
  * @return {Promise} []
  */
  async getbrandinfoAction(){
    let brandid = this.post('brandid');
    let shopid = this.post('shopid');

    let info = await this.model().query(' SELECT ob.name as brandname,of.name as factoryname FROM `think_or_brand` ob LEFT JOIN `think_or_factory` of ON ob.belongs = of.id WHERE ob.id = '+brandid+' ');
    const mypath = think.config('mypath');
    let getapi = this.controller(mypath+'/database');
    let goodsnum = await getapi.getbrandinfo(brandid, shopid);//记录数量
    let subjectnum = await getapi.getbrandsubjectnum(brandid, shopid);//记录数量

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info,'goodsnum':goodsnum,'subjectnum':subjectnum});
  }

  //品牌下的产品列表
  async getbrandgoodslistAction(){
    let brandid = this.post('brandid');
    let shopid = this.post('shopid');

    let info = await this.model().query(' SELECT g.* FROM `think_or_shop_goods` sg left join `think_or_goods` g on sg.goodsid = g.id WHERE sg.shopid = ' + shopid + ' and g.brandid = ' + brandid);
    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info});  
  }

  //品牌下的项目列表
  async getbrandsubjectlistAction(){
    let brandid = this.post('brandid');
    let shopid = this.post('shopid');

    let info = await this.model().query(' SELECT s.* FROM `think_or_shop_subject` os left join `think_or_subject` s on os.subjectid = s.id WHERE os.shopid = ' + shopid + ' and s.brandid = ' + brandid);
    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info});  
  }

  //品牌普及客户 
  async getbrandpujilistAction(){
    let shopid = this.post('shopid');
    let brandid = this.post('brandid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');

    let info = await this.model().query('SELECT og.id,og.name,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON oso.guestid = og.id WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.brandid = '+brandid+' GROUP BY osoi.brandid,oso.guestid');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }

  //品牌未普及客户
  async getbrandnopujilistAction(){
    let shopid = this.post('shopid');
    let brandid = this.post('brandid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');
    let info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = '+shopid+' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id  WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.brandid = '+brandid+' GROUP BY osoi.brandid,oso.guestid) ');
    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
    }

    //获取卡片信息
    async getcardinfoAction(){
    let cardid = this.post('cardid');
    let info = await this.model().query(' SELECT  * FROM `think_or_shop_card` WHERE id = '+cardid+' ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }

  //获取卡片普及客户信息
  async getcardpujilistAction(){
    let shopid = this.post('shopid');
    let cardid = this.post('cardid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');

    let info = await this.model().query(' SELECT og.name,og.id,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON og.id = oso.guestid WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.type = 3 AND osoi.typeid = '+cardid+' GROUP BY oso.guestid  ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }

  //卡类未普及客户
  async getcardnopujilistAction(){
    let shopid = this.post('shopid');
    let cardid = this.post('cardid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');

    let info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = '+shopid+' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.type = 3 AND osoi.typeid = '+cardid+' GROUP BY oso.guestid  ) ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }

  //获取项目信息
  async getsubjectinfoAction(){
    let subjectid = this.post('subjectid');

    let info = await this.model().query(' SELECT  os.name as subjectname,ob.name as brandname FROM `think_or_subject` os LEFT JOIN `think_or_brand` ob ON os.brandid = ob.id WHERE os.id = '+subjectid+' ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info});  
  }

  //获取项目的普及客户
  async getsubjectpujilistAction(){
    let shopid = this.post('shopid');
    let subjectid = this.post('subjectid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');

    let info = await this.model().query(' SELECT og.name,og.id,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON og.id = oso.guestid WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.type = 2 AND osoi.typeid = '+subjectid+' GROUP BY oso.guestid  ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }


  //获取项目未普及客户
  async getsubjectnopujilistAction(){
    let shopid = this.post('shopid');
    let subjectid = this.post('subjectid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');

    let info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = '+shopid+' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.type = 2 AND osoi.typeid = '+subjectid+' GROUP BY oso.guestid  ) ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }

  //获取家居信息
  async getgoodsinfoAction(){
    let goodsid = this.post('goodsid');
    let info = await this.model().query(' SELECT og.name as goodsname,ob.name as brandname FROM `think_or_goods` og LEFT JOIN `think_or_brand` ob ON og.brandid = ob.id WHERE og.id = '+goodsid+' ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info});  
  }

  //获取家居普及客户
  async getgoodspujilistAction(){
    let shopid = this.post('shopid');
    let goodsid = this.post('goodsid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');

    let info = await this.model().query(' SELECT og.name,og.id,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON og.id = oso.guestid WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.type = 1 AND osoi.typeid = '+goodsid+' GROUP BY oso.guestid  ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }

  //获取家居未普及客户
  async getgoodsnopujilistAction(){
    let shopid = this.post('shopid');
    let goodsid = this.post('goodsid');
    let start_time = this.post('start_time_js');
    let end_time = this.post('end_time_js');

    let info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = '+shopid+' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id WHERE oso.addtime >= '+start_time+' AND oso.addtime < '+end_time+' AND osoi.departmentid = '+shopid+' AND osoi.type = 1 AND osoi.typeid = '+goodsid+' GROUP BY oso.guestid  ) ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':info}); 
  }



};
